package com.xia.netty.thread.volatileExp;

public class VolatileExp {

   volatile   int a = 1;
    volatile  int b = 2;

    private  void exchange() {
        a = 3;
        b = a;
    }

    private   void  print() {
//        String s ;
//        synchronized (VolatileExp.class){
//        s =   a + "----" + b ;
//            System.out.println(s);
//        }

        System.out.println("b="+b+";a="+a);
    }

    public static void main(String[] args) {
        while (true) {
            final VolatileExp volatileExp = new VolatileExp();
            new Thread(() -> {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                volatileExp.exchange();
            }).start();

            new Thread(() -> {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                volatileExp.print();
            }).start();
        }
    }

}
